package com.itheima.store.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.itheima.store.domain.Post;
import com.itheima.store.domain.Order;
import com.itheima.store.domain.OrderItem;
import com.itheima.store.domain.PageBean;
import com.itheima.store.domain.Product;

import com.itheima.store.service.DeptManagerService;
import com.itheima.store.service.OrderService;
import com.itheima.store.service.PostManagerService;
import com.itheima.store.service.ProductService;
import com.itheima.store.utils.BaseServlet;
import com.itheima.store.utils.BeanFactory;
import com.itheima.store.utils.UUIDUtils;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

/**
 * Servlet implementation class AdminOrderServlet
 */
public class PostManagerServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	public String findAllPost(HttpServletRequest req ,HttpServletResponse resp) {
		try {
		//获取参数state
		//String state = req.getParameter("state");
		PostManagerService postManagerService =(PostManagerService)BeanFactory.getBean("postManagerService");
		List<Post> list =null;
		list = postManagerService.findAllPost();
			
		//2.数据存放到request域 
		req.setAttribute("list", list);
		//3.转发到显示的页面 admin/order/list.jsp
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "admin/dpmanager/post_list.jsp";
		
	}
	
	public String savePost(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String post_name  = request.getParameter("post_name");
		PostManagerService postManagerService =(PostManagerService)BeanFactory.getBean("postManagerService");
		String post_id = UUIDUtils.getUUID();
		Post post = new Post();
		post.setPost_id(post_id);
		post.setPost_name(post_name);
		
		try {
			postManagerService.savePost(post);
			response.sendRedirect(request.getContextPath()+"/PostManagerServlet?method=findAllPost");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String queryPost(HttpServletRequest req,HttpServletResponse resp){
		try{
			// 接收参数:
			String post_id = req.getParameter("post_id");
			// 调用业务层:
			PostManagerService postManagerService =(PostManagerService)BeanFactory.getBean("postManagerService");
			
			Post post = postManagerService.queryPost(post_id);
			
			req.setAttribute("post", post);
			return "admin/dpmanager/post_edit.jsp";
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException();
		}
		
	}
	
	
	public String updatePost(HttpServletRequest request,HttpServletResponse response){
		try{
			// 接收参数:
			String post_id = request.getParameter("post_id");
			String post_name = request.getParameter("post_name");
			Post post = new Post();
			post.setPost_id(post_id);
			post.setPost_name(post_name);
			// 调用业务层:
			PostManagerService postManagerService =(PostManagerService)BeanFactory.getBean("postManagerService");
			postManagerService.updatePost(post);
			
			response.sendRedirect(request.getContextPath()+"/PostManagerServlet?method=findAllPost");
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException();
		}
		return null;
	}
	
	public String deletePost(HttpServletRequest request,HttpServletResponse response){
		try{
			// 接收参数:
			String post_id = request.getParameter("post_id");
			// 调用业务层:
			PostManagerService postManagerService =(PostManagerService)BeanFactory.getBean("postManagerService");
			postManagerService.deletePost(post_id);
			
			response.sendRedirect(request.getContextPath()+"/PostManagerServlet?method=findAllPost");
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException();
		}
		return null;
	}
	
	

}
